package java13;



import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

class Student {
    private int id;
    private String name;
    private int age;
    private double grade;
    
    public Student(){
        
    }
    public Student(int id, String name, int age, double grade) {
        this.id = id;
        this.setName(name);
        this.setAge(age);
        this.setGrade(grade);
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        if (name.length()>10){
            throw new IllegalArgumentException("name's length should <=10 "+name.length());
        }
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if (age<=0){
            throw new IllegalArgumentException("age should >0 "+age);
        }
        this.age = age;
    }
    public double getGrade() {
        return grade;
    }
    public void setGrade(double grade) {
        if (grade<0 || grade >100){
            throw new IllegalArgumentException("grade should be in [0,100] "+grade);
        }
        this.grade = grade;
    }
     public void writeData(PrintWriter out)
       {
         
          
          out.println(this.id + "|" + this.name + "|" + this.age+ "|"
                + this.grade);
       }

       /**
        * Reads employee data from a buffered reader
        * @param in the scanner
        */
       public void readData(Scanner in)
       {
          String line = in.nextLine();
          String[] tokens = line.split("\\|");
          id = Integer.parseInt(tokens[0]);
          name = tokens[1];
          age = Integer.parseInt(tokens[2]);
          grade = Double.parseDouble(tokens[3]);
          
         Student stu = new Student(id, name, age,grade);
        
       }
    @Override 
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
    }

    
}
public class TextFileTest
{
   public static void main(String[] args)
   {
      Student[] stu = new Student[3];

      stu[0] = new Student(1, "chen", 19, 15);
      stu[1] = new Student(2, "huang",20, 10);
      stu[2] = new Student(3, "qiu",21, 16);

      try
      {
         // save all employee records to the file employee.dat
         PrintWriter out = new PrintWriter("student.dat");
         writeData(stu, out);
         out.close();

         // retrieve all records into a new array
         Scanner in = new Scanner(new FileReader("student.dat"));
         Student[] stu1 = readData(in);
         in.close();

         // print the newly read employee records
         for (Student e : stu1)
            System.out.println(e);
      }
      catch (IOException exception)
      {
         exception.printStackTrace();
      }
   }

   /**
    * Writes all employees in an array to a print writer
    * @param employees an array of employees
    * @param out a print writer
    */
   private static void writeData(Student[] stu, PrintWriter out) throws IOException
   {
      // write number of employees
      out.println(stu.length);

      for (Student e : stu)
         e.writeData(out);
   }

   /**
    * Reads an array of employees from a scanner
    * @param in the scanner
    * @return the array of employees
    */
   private static Student[] readData(Scanner in)
   {
      // retrieve the array size
      int n = in.nextInt();
      in.nextLine(); // consume newline

      Student[]stu = new Student[n];
      for (int i = 0; i < n; i++)
      {
         stu[i] = new Student();
         stu[i].readData(in);
      }
      return stu;
   }
}



